package com.ruoyi.system.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.SysQuestionRecordMapper;
import com.ruoyi.common.core.domain.entity.SysQuestionRecord;
import com.ruoyi.system.service.ISysQuestionRecordService;
import utils.MapUtil;

/**
 * 问题回答记录Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-05-22
 */
@Service
public class SysQuestionRecordServiceImpl implements ISysQuestionRecordService 
{
    @Autowired
    private SysQuestionRecordMapper sysQuestionRecordMapper;

    /**
     * 查询问题回答记录
     * 
     * @param questionId 问题回答记录主键
     * @return 问题回答记录
     */
    @Override
    public SysQuestionRecord selectSysQuestionRecordByQuestionId(Long questionId)
    {
        return sysQuestionRecordMapper.selectSysQuestionRecordByQuestionId(questionId);
    }

    /**
     * 查询问题回答记录列表
     * 
     * @param sysQuestionRecord 问题回答记录
     * @return 问题回答记录
     */
    @Override
    public List<SysQuestionRecord> selectSysQuestionRecordList(SysQuestionRecord sysQuestionRecord)
    {
        return sysQuestionRecordMapper.selectSysQuestionRecordList(sysQuestionRecord);
    }

    /**
     * 新增问题回答记录
     * 
     * @param sysQuestionRecord 问题回答记录
     * @return 结果
     */
    @Override
    public int insertSysQuestionRecord(SysQuestionRecord sysQuestionRecord)
    {

        SysQuestionRecord tsysQuestionRecord=sysQuestionRecordMapper.selectSysQuestionRecordByIds(sysQuestionRecord);
        int rows;
        if(StringUtils.isNotNull(tsysQuestionRecord)){
            sysQuestionRecord.setUpdateBy(sysQuestionRecord.getCreateBy());
            sysQuestionRecord.setCreateBy(null);
            sysQuestionRecord.setUpdateTime(DateUtils.getNowDate());
            rows = sysQuestionRecordMapper.updateSysQuestionRecord(sysQuestionRecord);
        }else{
            sysQuestionRecord.setCreateTime(DateUtils.getNowDate());
            rows = sysQuestionRecordMapper.insertSysQuestionRecord(sysQuestionRecord);
        }

        return rows;
    }

    /**
     * 修改问题回答记录
     * 
     * @param sysQuestionRecord 问题回答记录
     * @return 结果
     */
    @Override
    public int updateSysQuestionRecord(SysQuestionRecord sysQuestionRecord)
    {
        SysQuestionRecord tsysQuestionRecord=sysQuestionRecordMapper.selectSysQuestionRecordByIds(sysQuestionRecord);
        int rows;
        if(StringUtils.isNotNull(tsysQuestionRecord)){
            sysQuestionRecord.setUpdateTime(DateUtils.getNowDate());
            rows = sysQuestionRecordMapper.updateSysQuestionRecord(sysQuestionRecord);
        }else{
            sysQuestionRecord.setCreateTime(DateUtils.getNowDate());
            rows = sysQuestionRecordMapper.insertSysQuestionRecord(sysQuestionRecord);
        }
        return rows;
    }

    /**
     * 批量删除问题回答记录
     * 
     * @param questionIds 需要删除的问题回答记录主键
     * @return 结果
     */
    @Override
    public int deleteSysQuestionRecordByQuestionIds(Long[] questionIds)
    {
        return sysQuestionRecordMapper.deleteSysQuestionRecordByQuestionIds(questionIds);
    }

    /**
     * 删除问题回答记录信息
     * 
     * @param questionId 问题回答记录主键
     * @return 结果
     */
    @Override
    public int deleteSysQuestionRecordByQuestionId(Long questionId)
    {
        return sysQuestionRecordMapper.deleteSysQuestionRecordByQuestionId(questionId);
    }

    @Override
    public void mergeRecordsForGroupLeader(Long companyId, Long naireId) {
        SysQuestionRecord query=new SysQuestionRecord();
        query.setNaireId(naireId);
        query.setCompanyId(companyId);
        List<SysQuestionRecord> records=selectSysQuestionRecordList(query);
        Long userId=SecurityUtils.getUserId();
        List<Long> ownRecords=new ArrayList<>();
        HashMap<Long,HashMap<String,Integer>> answers=new HashMap<>();
        for (SysQuestionRecord record:records){
            if(record.getUserId().equals(userId)){
                ownRecords.add(record.getQuestionId());
                continue;
            }
            if(record.getAnswer()==null||record.getAnswer()=="")
                continue;
            if(answers.containsKey(record.getQuestionId())){
                HashMap<String,Integer> tanswers=answers.get(record.getQuestionId());
                if(tanswers.containsKey(record.getAnswer())){
                    tanswers.put(record.getAnswer(),tanswers.get(record.getAnswer())+1);
                }else{
                    tanswers.put(record.getAnswer(),1);
                }

            }else{
                HashMap<String,Integer> tanswers=new HashMap<>();
                tanswers.put(record.getAnswer(),1);
                answers.put(record.getQuestionId(), tanswers);
            }
        }

        for(Long questionId:answers.keySet()){
            if(ownRecords.contains(questionId))
                continue;
            SysQuestionRecord insert=new SysQuestionRecord();
            insert.setCompanyId(companyId);
            insert.setNaireId(naireId);
            insert.setQuestionId(questionId);
            insert.setUserId(userId);
            insert.setAnswer(MapUtil.getKeyWithMaxValue(answers.get(questionId)));
            insertSysQuestionRecord(insert);
        }
    }
}
